<?php

/* VIP */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class VipController extends AdminBaseController
{
    public function index()
    {
        
        $list = Db::name('vip')
            ->order("list_order asc,id desc")
            ->paginate(20);
        $list->each(function($v, $k){
            
            $length=$v['length']/(60*60*24*30);
           
            $v['length']=$length.'个月';
            
            return $v;
        });
        
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);

        return $this->fetch();
    }


    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            
            $name=$data['name'];
            if($name == ''){
                $this->error('请输入名称');
            }else{
                $check = Db::name('vip')->where("name='{$name}'")->find();
                if($check){
                    $this->error('名称已存在');
                }
            }
            
            
            $coin=$data['coin'];
            $coin_money=$data['coin_money'];
            
            if($coin<=0){
                $this->error('请输入正确价格');
            }

            if($coin_money<=0){
                $this->error('请输入正确价格-CNY');
            }
            
            $id = DB::name('vip')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->resetcache();
            $this->success("添加成功！");
        }
    }

    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('vip')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $length=$data['length']/(60*60*24*30);
           
        $data['length']=$length.'个月';
        
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();

            $id=$data['id'];
            $name=$data['name'];
            if($name == ''){
                $this->error('请输入名称');
            }else{
                $check = Db::name('vip')->where("name='{$name}' and id!={$id}")->find();
                if($check){
                    $this->error('名称已存在');
                }
            }
            
            
            $coin=$data['coin'];
            $coin_money=$data['coin_money'];
            
            if($coin<=0){
                $this->error('请输入正确价格');
            }

            if($coin_money<=0){
                $this->error('请输入正确价格-CNY');
            }
            
            $rs = DB::name('vip')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->resetcache();
            $this->success("保存成功！");
        }
    }

    public function listOrder()
    {
        $model = DB::name('vip');
        parent::listOrders($model);
        //$this->resetcache();
        $this->success("排序更新成功！");
    }
    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = DB::name('vip')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("vip/index"));
    }


    protected function resetcache(){
        $key='vip';

        $list=DB::name('vip')
                ->order('list_order asc')
                ->select();

        if($list){
            setcaches($key,$list);
        }
    }
}